<?php

class Users extends Controller {

    function __construct() {
        parent::Controller();

        $this->load->library('session');

        $this->load->model('Auth');
        $ckeck = $this->Auth->login($this->session->userdata('login'));


        if ($this->session->userdata('hash') != '' && $ckeck->Hash === $this->session->userdata('hash'))
           return;

        if ($this->uri->segment(2) === 'login')
            return;

        redirect('autorize/login');
    }

    function index() {

        $this->load->library('pagination');
        $this->load->model('Users_model', 'Users');

        $data['title'] = 'Список Юзерів';

        $data['streets'] = $this->Users->showStreets();
        $data['tariffs'] = $this->Users->showTariffs();
        $data['url'] = base_url();
        $config['base_url'] = $data['url'] . 'users/page/';
        $this->config->item('sgconf_xml');
        //$data ['js_datepicker'] = '<script src="' . $data['url'] . 'js/date/date.js" type="text/javascript"></script>';
        $data ['js_autocomplete'] = '<script src="' . $data['url'] . 'js/autocomplete.js" type="text/javascript"></script>';
        // $data ['css_jqueryui'] = '<link rel="stylesheet" href="' . $data['url'] . 'css/date/redmond/jquery-ui-1.8.5.custom.css" type="text/css" />';
        $data['js_perpage'] = "<script>function per_page( id ){document.cookie=\"per_page=\" + id +  \"; path=/; expires=\";window.location.reload();}
                                function sort(id,type){document.cookie=\"sort=\" + type +  \"; path=/; expires=\";document.cookie=\"order=\" + id +  \"; path=/; expires=\";window.location.reload();}</script>";
        //$data['sort']['login']= 1;
        //$data['js_form'] = '<script src="' . $data['url'] . 'js/jquery.form.js" type="text/javascript"></script>';
        // $data['js_validate'] = '<script src="' . $data['url'] . 'js/jquery-validate/jquery.validate.js" type="text/javascript"></script>';
        $data['js_myfunc'] = '<script src="' . $data['url'] . 'js/myfunc.js" type="text/javascript"></script>';
        $data['js_input'] = '<script src="jquery.maskedinput.js" type="text/javascript"></script>';
        $config['first_link'] = $this->lang->line('first_page');
        $config['last_link'] = $this->lang->line('last_page');
        $config['cur_tag_open'] = '<b style="color:red">';
        $config['cur_tag_close'] = '</b>';
        //$config['num_tag_open'] = '<div>';
        //$config['num_tag_close'] = '</div>';

        $config['per_page'] = (isset($_COOKIE ['per_page']) ? $_COOKIE ['per_page'] : 100);

        if ($this->uri->segment(3)) {
            $start = $this->uri->segment(3);
        } else {

            $start = 0;
        }

        $data['users'] = $this->Users->showUsers($config['per_page'], $start, $count_all);

//        print $count_all."rr";

        $config['total_rows'] = $count_all;
        // $config['uri_segment'] = 2;
//print  $this-> uri-> segment(1)."ff";

        $this->pagination->initialize($config);


        $data['pages'] = $this->pagination->create_links();

        $data['segment'] = $this->uri->segment(1);
        $this->load->view('header', $data);
        $this->load->view('menu', $data);
        $this->load->view('users', $data);
        $this->load->view('footer', $data);
    }

    function page() {
        return $this->index();
    }

    function search() {

$this->load->model('Users_model', 'Users');

        $data['search'] = $this->Users->autocomplete($this->uri->segment(3), $this->uri->segment(4));

//    print_r($users);
        $this->load->view('search', $data);
        /*
          foreach ( as $users) {

          echo $users['Realname']."<br>";

          }
         */
    }

    function delivery() {

        $data['url'] = base_url();
        $this->load->view('delivery', $data);
    }

    function ping() {

        if (isset($_POST['ping'])) {

            $data['result'] = preg_replace("#\n#Uis", '<br>', shell_exec('ping ' . $_POST['ping']));
        } elseif (isset($_POST['nmap'])) {

            $data['result'] = preg_replace("#host up#Uis", '<b>host up</b>', shell_exec('nmap ' . $_POST['nmap']));
        }



        $this->load->view('ping', $data);
    }

    function pay() {

    }

    function freezing() {

        $this->load->model('Users_model', 'Users');
        $data['freezing'] = $this->Users->freezing('show', $this->uri->segment(3), $date_start, $date_end);
        $this->load->view('freezing', $data);
    }

    function tasks() {
        $this->load->model('Users_model', 'Users');
        $data['statuses'] = $this->Users->getStatuses();
        $data['tasks'] = $this->Users->getTasksUser($this->uri->segment(3));
        $this->load->view('tasks', $data);
    }

}

